<?php
/**
 * Created by PhpStorm.
 * User: denh
 * Date: 17/2/6
 * Time: 下午4:00
 */
namespace tools;

class redis {

    public $client;
    public $pre;

    private function __construct() {
        $this->pre = 'GZH_'.G_SERV_ID;
        $this->connect();
    }

    public static  function instance() {
        static $instance;
        if (empty($instance)) {
            $instance = new redis();
        }
        return $instance;
    }

    function connect() {
        global $G_CONFIG;
        $this->client = new \Redis();
        $this->client->connect($G_CONFIG['redis']['host'], $G_CONFIG['redis']['port']);
        $this->client->auth($G_CONFIG['redis']['pwd']);
    }

    function get($key) {
        $key = $this->pre .'_'. $key;
        return $this->client->get($key);
    }

    function set($key, $value, $expire=0) {
        $key =  $this->pre .'_'. $key;
        return $this->client->set($key, $value, $expire);
    }

    /**
     * 返回一天中剩余的秒数
     * @return int
     */
    function residueTime() {
        $current_time = time();
        $nextDayTime = $current_time + 3600*24;
        $residueTime = strtotime(date('Y-m-d', $nextDayTime)) - $current_time;
        return $residueTime;
    }
}